package com.cloudcentral.controller;

import com.cloudcentral.entity.Menu;
import com.cloudcentral.entity.vo.MenuTreeVO;
import com.cloudcentral.exception.InvalidException;
import com.cloudcentral.service.MenuService;
import com.cloudcentral.util.Result;
import com.cloudcentral.util.WebUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 系统菜单表 前端控制器
 * </p>
 *
 * @author Administrator
 * @since 2023-10-27
 */
@Slf4j
@RestController
@RequestMapping("/menu")
@Tag(name = "菜单管理")
public class MenuController extends BaseController {
  private final MenuService menuService;

  @Autowired
  public MenuController(MenuService menuService) {
    this.menuService = menuService;
  }

  @PostMapping("/submit")
  @Operation(summary = "提交")
  public Result<Menu> submit(Menu menu) throws Exception {
    return Result.ok(menuService.submit(menu));
  }

  @DeleteMapping("/delete")
  @Operation(summary = "删除")
  public Result<Boolean> delete(Long id) throws Exception {
    return Result.ok(menuService.delete(id));
  }

  @GetMapping("/select")
  @Operation(summary = "查询")
  public Result<Menu> select(Long id) {
    return Result.ok(menuService.getById(id));
  }

  @GetMapping("/listTree")
  @Operation(summary = "树状集合")
  public Result<List<MenuTreeVO>> listTree(Long platformId) throws InvalidException {
    List<Menu> menuList = menuService.listByUser(platformId, WebUtils.currentUser().getId());
    List<MenuTreeVO> menuTreeVOList = MenuTreeVO.assemble(menuList, null);
    return Result.ok(menuTreeVOList);
  }
}
